package com.qimei.onerice.servlet;

import com.google.gson.Gson;
import com.qimei.onerice.entity.Admin;
import com.qimei.onerice.service.AdminService;
import com.qimei.onerice.service.impl.AdminServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
	AdminService adminService = new AdminServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");

		String action = request.getParameter("action");

		if ("getAll".equals(action)) {
			// 获取所有管理员数据
			handleGetList(request, response);
		} else if ("getById".equals(action)) {
			// 获取单个管理员数据
			handleGetById(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("未知的操作");
		}
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String action = request.getParameter("action");

		System.out.println(action);
		if ("add".equals(action)) {
			// 添加管理员
			handleAdd(request, response);
		} else if ("update".equals(action)) {
			// 更新管理员
			handleUpdate(request, response);
		} else if ("delete".equals(action)) {
			// 删除管理员
			handleDelete(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("未知操作");
		}
	}
	// 添加新管理员
	private void handleAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String adminName = request.getParameter("adminName");
		String adminUserName = request.getParameter("adminUsername");
		String adminEmail = request.getParameter("adminEmail");
		String adminPhone = request.getParameter("adminPhone");

		// 创建一个 Admin 对象
		Admin newAdmin = new Admin();
		newAdmin.setAdminName(adminName);
		newAdmin.setAdminPhone(adminPhone);
		newAdmin.setAdminUsername(adminUserName);
		newAdmin.setAdminEmail(adminEmail);

		// 调用服务方法添加新管理员
		boolean success = adminService.addAdmin(newAdmin);

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 更新管理员
	private void handleUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String adminIdParam = request.getParameter("adminId");
		int adminId = Integer.parseInt(adminIdParam);
		String adminName = request.getParameter("adminName");
		String adminUserName = request.getParameter("adminUsername");
		String adminEmail = request.getParameter("adminEmail");
		String adminPhone = request.getParameter("adminPhone");

		Admin updatedAdmin = new AdminServiceImpl().getAdminById(adminId);
		updatedAdmin.setAdminId(adminId);
		updatedAdmin.setAdminName(adminName);
		updatedAdmin.setAdminUsername(adminUserName);
		updatedAdmin.setAdminEmail(adminEmail);
		updatedAdmin.setAdminPhone(adminPhone);

		// 调用服务方法更新管理员
		boolean success = adminService.updateAdmin(updatedAdmin);
		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 删除管理员
	private void handleDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String adminId = request.getParameter("adminId");

		// 调用服务方法删除管理员
		boolean success = adminService.deleteAdmin(Integer.parseInt(adminId));

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 根据ID查询管理员
	private void handleGetById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String adminIdParam = request.getParameter("adminId");

		int adminId = Integer.parseInt(adminIdParam);

		// 调用方法根据ID获取管理员
		Admin admin = adminService.getAdminById(adminId);

		// 将 Admin 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(admin);
		sendJsonResponse(response, jsonData);
	}


	private void handleGetList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Admin> adminList = adminService.getAdminList();
		// 将 Admin 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(adminList);
		sendJsonResponse(response, jsonData);
	}

	private void sendJsonResponse(HttpServletResponse response, String jsonData) throws IOException {
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(jsonData);
		out.flush();
	}
}
